﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace FizzBuzz
{
    [Serializable]
    class MBVClass
    {
        public string HomeApplicationDomain
        {
            get
            {
                return AppDomain.CurrentDomain.FriendlyName;
            }
        }
    }

    class MBRClass : MarshalByRefObject
    {
        public string HomeApplicationDomain
        {
            get
            {
                return AppDomain.CurrentDomain.FriendlyName;
            }
        }
    }


    class AppDomainSetupDemo
    {
        public void RunMarshalDemo()
        {
            AppDomain ad = AppDomain.CreateDomain("My new domain");
            MBVClass mbvObject = (MBVClass) ad.CreateInstanceFromAndUnwrap("FizzBuzz.exe",
                                                                           "FizzBuzz.MBVClass");

            MBRClass mbrObject = (MBRClass)ad.CreateInstanceFromAndUnwrap("FizzBuzz.exe",
                                                                           "FizzBuzz.MBRClass");

            Console.WriteLine("Main AppDomain:{0}", AppDomain.CurrentDomain.FriendlyName);
            Console.WriteLine("AppDomain of MBVClass:{0}", mbvObject.HomeApplicationDomain);
            Console.WriteLine("AppDomain of MBRClass:{0}", mbrObject.HomeApplicationDomain);
        }

        public void Run()
        {
            AppDomainSetup setupInfo = new AppDomainSetup();
            setupInfo.ApplicationBase = @"C:\temp";
            setupInfo.ConfigurationFile = "MyApp.config";
            setupInfo.PrivateBinPath = "bin";

            AppDomain newDomain = AppDomain.CreateDomain("My new domain", null, setupInfo);
        }
    }
}
